package com.goldgov.JiguangPush.web;

import java.net.URLDecoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.utils.JiguangPush;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 
 * @ClassName:InformationController
 * @Description: 信息采集
 * @author MaN
 * @date 2018年7月11日 
 */

@RestController
@RequestMapping("/portal/jiguang")
@Api("信息发布接口")
public class JiguangPushController {

	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@GetMapping("/push")
	@ApiOperation(value = "极光推送", notes ="")
	public JsonObject<Object> jiguangPush(@ApiParam(value = "党员ID") @RequestParam("userID") String userID,@ApiParam(value = "通知标题") @RequestParam("modelName") String modelName,@ApiParam(value = "通知内容") @RequestParam("content")String content,@ApiParam(value = "通知模板") @RequestParam("mapString")String mapString,HttpServletRequest request){
//		mapString="{\"noticeType\":\"12\",\"id\":\"hanxiangyu\",\"noticeId\":\"be79e269-8afe-11e8-bb5f-0242ac130005\"}";
		for (int i = 0; i < 16; i++) {
		    for (int j = 0; j < 16; j++) { 
		        int code = i * 16 + j; 
		        System.out.printf("\u001b[38;5;%dm%-4d", code, code); 
		    } 
		    System.out.println("\u001b[0m"); 
		}
		try {
			@SuppressWarnings("unchecked")
			Map<String,String>  params=JSONObject.parseObject(URLDecoder.decode(mapString,"UTF-8"), Map.class);
			JiguangPush.push(userID,modelName,content, params);
		}catch(Exception e){
			logger.info("极光推送error",e);
		}
		return JsonSuccessObject.SUCCESS;
	}
	
	/**
	 * 自定义极光推送 不在状态栏显示
	 * @date 2018年9月18日 上午9:55:38 
	 * @author hanxiangyu
	 * @param userID
	 * @param modelName
	 * @param content
	 * @param mapString
	 * @param request
	 * @return
	 */
	@GetMapping("/customJpush")
	@ApiOperation(value = "极光推送", notes ="")
	public JsonObject<Object> customJpush(@ApiParam(value = "党员ID(通知人员标识)") @RequestParam("userID") String userID,@ApiParam(value = "通知标题") @RequestParam("modelName") String modelName,@ApiParam(value = "通知内容") @RequestParam("content")String content,@ApiParam(value = "通知需要的其他参数") @RequestParam("mapString")String mapString,HttpServletRequest request){
		for (int i = 0; i < 16; i++) {
			for (int j = 0; j < 16; j++) { 
				int code = i * 16 + j; 
				System.out.printf("\u001b[38;5;%dm%-4d", code, code); 
			} 
			System.out.println("\u001b[0m"); 
		}
		try {
			@SuppressWarnings("unchecked")
			Map<String,String>  params=JSONObject.parseObject(URLDecoder.decode(mapString,"UTF-8"), Map.class);
			JiguangPush.customPush(userID,modelName,content, params);
		}catch(Exception e){
			logger.info("极光推送error",e);
		}
		return JsonSuccessObject.SUCCESS;
	}
	/**
	 * 推送给所有安装app的人
	 * @date 2018年9月18日 上午9:55:38 
	 * @author hanxiangyu
	 * @param userID
	 * @param modelName
	 * @param content
	 * @param mapString
	 * @param request
	 * @return
	 */
	@GetMapping("/unifyJpush")
	@ApiOperation(value = "极光推送", notes ="")
	public JsonObject<Object> unifyJpush(@ApiParam(value = "通知标题") @RequestParam("modelName") String modelName,
			@ApiParam(value = "通知内容") @RequestParam("content")String content,
			@ApiParam(value = "通知需要的其他参数") @RequestParam("mapString")String mapString,HttpServletRequest request){
		for (int i = 0; i < 16; i++) {
			for (int j = 0; j < 16; j++) { 
				int code = i * 16 + j; 
				System.out.printf("\u001b[38;5;%dm%-4d", code, code); 
			} 
			System.out.println("\u001b[0m"); 
		}
		try {
			@SuppressWarnings("unchecked")
			Map<String,String>  params=JSONObject.parseObject(URLDecoder.decode(mapString,"UTF-8"), Map.class);
			JiguangPush.unifyPush(modelName,content, params);
		}catch(Exception e){
			logger.info("极光推送error",e);
		}
		return JsonSuccessObject.SUCCESS;
	}
}